package com.jtlrm.ckd.huanxin;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.alibaba.android.arouter.launcher.ARouter;
import com.base.sdk.base.net.CommonObserver;
import com.hyphenate.chatuidemo.db.NewUserDao;
import com.hyphenate.chatuidemo.domain.NewUserEntity;
import com.hyphenate.chatuidemo.ui.GroupsActivity;
import com.jtlrm.ckd.R;
import com.jtlrm.ckd.base.fragment.BaseFragment;
import com.jtlrm.ckd.entity.GroupMemberEntity;
import com.jtlrm.ckd.entity.HuanZheEntity;
import com.jtlrm.ckd.entity.PageEntity;
import com.jtlrm.ckd.entity.UserEntity;
import com.jtlrm.ckd.mvp.model.SuiFangModel;
import com.jtlrm.ckd.mvp.model.dao.UserHelper;
import com.jtlrm.ckd.mvp.view.activity.CreateGroupActivity;
import com.jtlrm.ckd.mvp.view.activity.MyGrroupsActivity;
import com.jtlrm.ckd.mvp.view.activity.MyWorkerActivity;
import com.jtlrm.ckd.util.ContactList.ContactAdapter;
import com.jtlrm.ckd.util.ContactList.ContactListView;
import com.jtlrm.ckd.util.ContactList.SlideBarEntity;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadmoreListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * 通讯录
 */
public class MyContactListFragment extends BaseFragment {

    @BindView(R.id.my_contact_list)
    ContactListView contactListView;
    protected ListView listView;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout smartRefreshLayout;
    SuiFangModel suiFangModel;
    List<HuanZheEntity> userData = new ArrayList<>();
    NewUserDao userDao = new NewUserDao();
    public static MyContactListFragment newInstance() {
        MyContactListFragment fragment = new MyContactListFragment();
        return fragment;
    }

    @Override
    protected int setContentLayout() {
        return R.layout.fragment_my_constact_list;
    }

    @Override
    protected void initView() {
        suiFangModel = new SuiFangModel();
        listView = contactListView.getListView();
        @SuppressLint("InflateParams") View headerView = LayoutInflater.from(getActivity()).inflate(R.layout.contacts_header, null);
        HeaderItemClickListener clickListener = new HeaderItemClickListener();
        headerView.findViewById(R.id.my_group_item).setOnClickListener(clickListener);
        headerView.findViewById(R.id.my_create_group_item).setOnClickListener(clickListener);
        headerView.findViewById(R.id.my_worker_item).setOnClickListener(clickListener);
        listView.addHeaderView(headerView);
    }

    @Override
    protected void obtainData() {
    }

    @Override
    protected void initEvent() {
        smartRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                loadData();
            }
        });
        smartRefreshLayout.setEnableLoadmore(false);
        smartRefreshLayout.autoRefresh();
    }

    private void loadData() {
        suiFangModel.queryHuanZheList(UserHelper.getInstance(context).getUserId()
                , new CommonObserver<List<HuanZheEntity>>() {

                    @Override
                    public void onError(int errType, String errMessage) {
                        showToast(errMessage);
                        closeFrshorLoadmore();
                    }

                    @Override
                    public void onResult(List<HuanZheEntity> data) {
                        userData.clear();
                        userData.addAll(data);
                        contactListView.init(getSlideList(data));
                        closeFrshorLoadmore();
                        contactListView.setItemClick(new ContactAdapter.ItemClick() {
                            @Override
                            public void itemClick(SlideBarEntity slideBarEntity) {
                                ARouter.getInstance().build("/person/info/chat")
                                        .withString("userId", slideBarEntity.getuId()).withString("imId", slideBarEntity.getiId()).navigation();
                            }
                        });
                        List<NewUserEntity> list =new ArrayList<>();
                        for (HuanZheEntity entity:data){
                            NewUserEntity userEntity = new NewUserEntity();
                            userEntity.setUserId(entity.getId());
                            userEntity.setImId(entity.getEasemobId());
                            userEntity.setAvatar(entity.getHeadPicture());
                            userEntity.setName(entity.getName());
                            userEntity.setRole("2");
                            list.add(userEntity);
                        }
                        userDao.saveContactList(list);
                    }
                }, lifecycleSubject);
    }

    protected class HeaderItemClickListener implements View.OnClickListener {

        @Override
        public void onClick(View v) {
            int id = v.getId();
            if (id == R.id.my_group_item) {
                // 进入群聊列表页面
                startActivity(new Intent(getActivity(), MyGrroupsActivity.class));
            } else if (id == R.id.my_worker_item) {
                // 我的同事
                startActivity(new Intent(getActivity(), MyWorkerActivity.class));
            } else if (id == R.id.my_create_group_item) {
                //进入创建群聊
                startActivity(new Intent(getActivity(), CreateGroupActivity.class));
            }
        }
    }

    public List<SlideBarEntity> getSlideList(List<? extends SlideBarEntity> list) {
        List<SlideBarEntity> data = new ArrayList<>();
        if (list != null) {
            for (SlideBarEntity entity : list) {
                data.add(entity);
            }
        }
        return data;
    }


    /**
     * 关闭加载更多或者刷新
     */
    private void closeFrshorLoadmore() {
        if (smartRefreshLayout.isLoading()) {
            smartRefreshLayout.finishLoadmore();
        }
        if (smartRefreshLayout.isRefreshing()) {
            smartRefreshLayout.finishRefresh();
        }
    }
}
